<?php

namespace Caifenglei\AuthRbac\Http\Requests;

use Caifenglei\AuthRbac\Models\Permission;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdatePermissionRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'parent_id' => 'required|integer',
            'label' => 'required|string|max:100',
            'name' => ['required', 'max:255', Rule::unique('permissions')->ignore($this->id)],
            'type' => [
                'required',
                'integer',
                Rule::in([Permission::TYPE_MENU, Permission::TYPE_BUTTON])
            ],
            'meta' => 'sometimes|nullable|string|max:512',
            'sort' => 'sometimes|integer|max:10000',
            'roles' => 'sometimes|array',
        ];
    }

    public function messages()
    {
        return [
            'parent_id.required' => '父级权限不能为空',
            'parent_id.integer' => '父级权限必须为整数',
            'label.required' => '权限名称不能为空',
            'label.string' => '权限名称必须为字符串',
            'label.max' => '权限名称长度不能超过100个字符',
            'name.required' => '权限标识不能为空',
            'name.max' => '权限标识长度不能超过255个字符',
            'type.required' => '权限类型不能为空',
            'type.integer' => '权限类型必须为整数',
            'type.in' => '权限类型必须为菜单或按钮',
        ];
    }
}
